<?php
    session_start();
    include("includes/connect_db.php");
    include("includes/function.php");
    $id_outbox = $_POST['id_outbox'];
?>

<style>
    #form-modal{
        top: 35%;
    }
</style>

<div id="form-modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3 id="myModalLabel">

            <?php
                $sql_cari_nomer = "select DestinationNumber from sentitems where ID = '$id_outbox'";
                $query_cari_nomer = mysql_query($sql_cari_nomer);
                $cari_nomer = mysql_fetch_array($query_cari_nomer);
                
                $sql_cek_kontak = "SELECT * FROM tb_kontak_rincian where no_hp='$cari_nomer[DestinationNumber]'";
                $query_cek_kontak = mysql_query($sql_cek_kontak);
                $banyak_row = mysql_num_rows($query_cek_kontak);

                if($banyak_row > 0){
                    echo "<a href='#' class='lihat_kontak'>";
                    $query_cek_kontak = mysql_query($sql_cek_kontak);
                    $cek_kontak = mysql_fetch_array($query_cek_kontak);
                        if($cek_kontak['no_hp'] == $cari_nomer['DestinationNumber']){
             ?>
                            <img src="<?php echo cekGambar($cek_kontak['id_kontak'], 1, 3); ?>" style="height: 40px; margin: 0 10px 0 0;" class="img-rounded">
            <?php
                            echo substr($cek_kontak['nama_rincian'], 0, 20);
                            if (strlen($cek_kontak['nama_rincian']) > 20) {
                                echo "...";
                            }
                            $temp = 0;
                             echo " (".$cek_kontak['no_hp'].")";
                        }else{
                            echo $cari_nomer['DestinationNumber'];
                            $temp = 1;
                        }
                    echo "</a>";
                } else{
                    echo $cari_nomer['DestinationNumber'];
                    $temp = 1;
                }
            ?>
        </h3>
    </div>
    <div class="modal-body">
    </div>
</div>


<script type="text/javascript">
        
    function loadOutbox(){
        var formOutbox = {
            id_outbox: <?php echo $_POST['id_outbox']; ?>,
            temp: <?php echo $temp; ?>
        };
        
        $.ajax({
            type: "POST",
            url: "outbox-modal-single.php",
            data: formOutbox, 
            cache: false, 
            success: function(msg){
                $(".modal-body").html(msg);
            } 
        });            
    }

    function loadConv(){
        var formConv = {
            id_outbox: <?php echo $_POST['id_outbox']; ?>,
            temp: <?php echo $temp; ?>
        };
        
        $.ajax({
            type: "POST",
            url: "outbox-modal-conv.php",
            data: formConv, 
            cache: false, 
            success: function(msg){
                $(".modal-body").html(msg);
            } 
        });            
    }

    function deleteMsg(){
        var formDelete = {
            id_outbox: <?php echo $_POST['id_outbox']; ?>
        };
        
        $.ajax({
            type: "POST",
            url: "act_outbox.php?aksi=3",
            data: formDelete, 
            cache: false, 
            success: function(msg){
                if (msg == 'sukses') {
                    location.reload();
                };
            } 
        });            
    }

    function deleteConv(){
        var formDeleteConv = {
            id_outbox: <?php echo $_POST['id_outbox']; ?>
        };
        
        $.ajax({
            type: "POST",
            url: "act_outbox.php?aksi=4",
            data: formDeleteConv, 
            cache: false, 
            success: function(msg){
                if (msg == 'sukses') {
                    location.reload();
                };
            } 
        });            
    }

    function replyMsgOutbox(){
        var formReply = {
            id_outbox: <?php echo $_POST['id_outbox']; ?>,
            reply_msg : $('#reply-msg').val()
        };
        
        $.ajax({
            type: "POST",
            url: "act_outbox.php?aksi=2",
            data: formReply, 
            cache: false, 
            success: function(msg){
                if (msg == 'sukses') {
                    location.reload();
                };
            } 
        });            
    }

    function detailKontak(){
        var formDetailKontak = {
            id_kontak: 
                <?php 
                    if (is_null($cek_kontak['id_kontak'])) {
                        echo "0";
                    } else{
                        echo $cek_kontak['id_kontak']; 
                    }
                ?>,
            tipe: "Kontak"
        };
        
        $.ajax({
            type: "POST",
            url: "kontak-modal.php",
            data: formDetailKontak, 
            cache: false, 
            success: function(msg){
                
                $("#tampil-modal-lain").html(msg);
            } 
        });            
    }
    
    $(document).ready(function(){
        $('#form-modal').modal('show');
        
        loadOutbox();
        
        $('#full-conv').live('click',function(){
            loadConv();
        });

        $('#delete-msg').live('click',function(){
            deleteMsg();
        });

        $('#reply').live('click',function(){
            tempView = $(this);
            id_outbox = <?php echo $_POST['id_outbox']; ?>;

            location.href = "compose.php?aksi=4&id="+id_outbox;
        });

        $('#forward').live('click',function(){
            tempView = $(this);
            id_outbox = <?php echo $_POST['id_outbox']; ?>;

            location.href = "compose.php?aksi=5&id="+id_outbox;
        });

        $('#form-modal').on('click', '.add-contact', function(){
            tempView = $(this);
            id_outbox = <?php echo $_POST['id_outbox']; ?>;

            $.ajax({ 
                type: "POST",
                url: "kontak-tambah-modal.php?id="+id_outbox+"&aksi=2",
                cache: false, 
                success: function(msg){ 
                    $("#tampil-modal-kontak").html(msg);
                    $("#tampil-modal-kontak").fadeIn("medium");
                } 
            });
        });

        $('#delete-conv').live('click',function(){
            deleteConv();
        });

        $('#single-msg').live('click',function(){
            loadOutbox();
        });

        $('#send-msg').live('click',function(){
            replyMsgOutbox();
        });     

        $('.lihat_kontak').click(function(){
            detailKontak();
        });  

        tempView.parent('tr').css('font-weight','normal');
    });
</script>